/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.synchronization;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JOptionPane;
import systole.synchronization.view.JDialogSynchronization;
import systole.synchronization.worker.SynchronizationWorker;
import systole.view.SystoleApp;
import systole.view.messages.DialogsMessages;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author jmj
 */
public class SyncController implements PropertyChangeListener {

    private JDialogSynchronization jDialogSyncronization;
    private SynchronizationWorker worker;
    private boolean running = false;

    public SyncController() {
        super();
    }

    public void runSynchronization() {
        this.jDialogSyncronization = new JDialogSynchronization(SystoleApp.getApplication().getMainFrame(), this);
        this.jDialogSyncronization.setResizable(false);
        this.jDialogSyncronization.setLocationRelativeTo(null);
        this.worker = new SynchronizationWorker(this);
        this.worker.addPropertyChangeListener(this);
        this.setRunning(true);
        this.worker.execute();
        this.jDialogSyncronization.setVisible(true);

    }

    /**
     *
     * @param running
     */
    private void setRunning(boolean running) {
        this.running = running;
        if (this.jDialogSyncronization != null) {
            this.jDialogSyncronization.getjXBusyLabel().setBusy(running);
        }
    }

    /**
     * @return the jDialogSyncronization
     */
    public JDialogSynchronization getjDialogSyncronization() {
        return jDialogSyncronization;
    }

    public void cancel() {
        if (this.jDialogSyncronization == null) {
            return;
        }
        if (this.running) {
            if (JOptionPane.OK_OPTION != JOptionPane.showOptionDialog(this.jDialogSyncronization,
                    DialogsMessages.CLOSE_SYNCHRONIZATION, DialogsMessages.DIALOG_BOX_TITLE,
                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    null, null)) {
                return;
            }
            this.setRunning(false);
            if (this.worker != null) {
                this.worker.cancel(false);
                return;
            }
        }
        this.jDialogSyncronization.setVisible(false);
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            this.jDialogSyncronization.updateProgressBar(progress);
        }
    }

    public void syncFinished() {
        JOptionPane.showOptionDialog(this.jDialogSyncronization,
                DialogsMessages.SYNCHRONIZATION_FINISHED, DialogsMessages.DIALOG_BOX_TITLE,
                JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE, null,
                null, null);
        this.jDialogSyncronization.setVisible(false);
    }

    public void syncCanceled() {
        this.jDialogSyncronization.setVisible(false);
    }

    public void syncWithError() {
        ErrorDialog.showError(jDialogSyncronization, "Ocurrió un error y se" + System.getProperty("line.separator") + "finalzó la sincronización");
        this.jDialogSyncronization.setVisible(false);
    }
}
